#include "synwit_ui_framework/synwit_ui.h"
#include "appkit/screen_id.h"
#include "appkit/screens/screen001.h"
#include "appkit/screens/screen002.h"
#include "swm34s/common/lcdc/dev_lcdc.h"
#include "SWM341_sfc.h"
#include "SWM341.h"
/*
 * 界面回调函数
 */

uint8_t km_MPH_state = 0;

void reverse(char *str) //反转字符算法
{  
    int len = strlen(str);  
    for (int i = 0; i < len / 2; i++) {  
        char temp = str[i];  
        str[i] = str[len - i - 1];  
        str[len - i - 1] = temp;  
    }  
}

void ShowPic(uint8_t num)
{
	  char speed[10];
		uint8_t speedF;
		uint8_t pic_num;
    lv_obj_t *Pic;
	
if(DX_flag)
{
//////////////////////////////隐藏画面////////////////////////////////////
			Pic = synwit_ui_find_lv_obj(WIDGET_LEVELIMG_2);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_ARTCHAR_3);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE4);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE7);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE11);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE12);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE13);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE14);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE15);
			lv_obj_set_hidden(Pic, true); //hidden
//////////////////////////////隐藏画面////////////////////////////////////

			
			if(system_fault_light)
			{
				Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE7);
				lv_obj_set_hidden(Pic, false); 
			}
			
			if(camera_fault_light)
			{
				Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE4);
				lv_obj_set_hidden(Pic, false); 
			}
				
			if(vision_state==0&&!vision_delay_flag)
			{
						Pic = synwit_ui_find_lv_obj(WIDGET_LEVELIMG_2);
						lv_obj_set_hidden(Pic, false); 
				
				/////////////////单位////////////////////////////
						speedF = car_speed_state/10;
						
						if(km_MPH_state==0)
							sprintf(speed, "%d", speedF);
						else
							sprintf(speed, "%d", (uint8_t)(speedF*0.62));
						
				    pic_num = speedF*40/MAX_SPEED;
				
						Pic = synwit_ui_find_lv_obj(WIDGET_ARTCHAR_3);
						lv_obj_set_hidden(Pic, false); 
						reverse(speed);//倒显示需反转字符
						lv_artchar_set_text(Pic, speed);
				
						Pic = synwit_ui_find_lv_obj(WIDGET_LEVELIMG_2);
						lv_levelimg_set_level(Pic, pic_num); //显示分级图像
						
						if(km_MPH_state==0)
								Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE12);
						else
							  Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE11);
						lv_obj_set_hidden(Pic, false); 
				/////////////////单位////////////////////////////
			}
			else
			{
						if(vision_state_temp==0x01)
						{
							Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE14);
							lv_obj_set_hidden(Pic, false); 
						}
						else if(vision_state_temp==0x02)
						{
							Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE15);
							lv_obj_set_hidden(Pic, false); 
						}
						else if(vision_state_temp==0x03)
						{
							Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE15);
							if(shanshuo_flag)
								lv_obj_set_hidden(Pic, false); 
							else
								lv_obj_set_hidden(Pic, true); 
						}
						else if(vision_state_temp==0x04)
						{
							Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE13);
							if(shanshuo_flag)
								lv_obj_set_hidden(Pic, false); 							else
								lv_obj_set_hidden(Pic, true); 
						}
			}
}
else
{
//////////////////////////////隐藏画面////////////////////////////////////
			Pic = synwit_ui_find_lv_obj(WIDGET_LEVELIMG_1);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_ARTCHAR_2);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE2);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE3);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE5);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE6);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE8);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE9);
			lv_obj_set_hidden(Pic, true); //hidden
			Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE10);
			lv_obj_set_hidden(Pic, true); //hidden
//////////////////////////////隐藏画面////////////////////////////////////

			
			if(system_fault_light)
			{
				Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE3);
				lv_obj_set_hidden(Pic, false); 
			}
			
			if(camera_fault_light)
			{
				Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE2);
				lv_obj_set_hidden(Pic, false); 
			}
				
			if(vision_state==0&&!vision_delay_flag)
			{
						Pic = synwit_ui_find_lv_obj(WIDGET_LEVELIMG_1);
						lv_obj_set_hidden(Pic, false); 
			
				/////////////////单位////////////////////////////
						
						speedF = car_speed_state/10;
						
						if(km_MPH_state==0)
							sprintf(speed, "%d", speedF);
						else
							sprintf(speed, "%d", (uint8_t)(speedF*0.62));
						
				    pic_num = speedF*40/MAX_SPEED;
				
						Pic = synwit_ui_find_lv_obj(WIDGET_ARTCHAR_2);
						lv_obj_set_hidden(Pic, false); 
						lv_artchar_set_text(Pic, speed);
				
						Pic = synwit_ui_find_lv_obj(WIDGET_LEVELIMG_1);
						lv_levelimg_set_level(Pic, pic_num); //显示分级图像
						
						if(km_MPH_state==0)
								Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE6);
						else
							  Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE5);
						lv_obj_set_hidden(Pic, false); 
				/////////////////单位////////////////////////////
			}
			else
			{
						if(vision_state_temp==0x01)
						{
							Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE9);
							lv_obj_set_hidden(Pic, false); 
						}
						else if(vision_state_temp==0x02)
						{
							Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE10);
							lv_obj_set_hidden(Pic, false); 
						}
						else if(vision_state_temp==0x03)
						{
							Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE10);
							if(shanshuo_flag)
								lv_obj_set_hidden(Pic, false); 
							else
								lv_obj_set_hidden(Pic, true); 
						}
						else if(vision_state_temp==0x04)
						{
							Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE8);
							if(shanshuo_flag)
								lv_obj_set_hidden(Pic, false); 
							else
								lv_obj_set_hidden(Pic, true); 
						}
			}
}
}


static void screen001_on_timer(void *user_data)
{
	if(DX_flag)
	{
		synwit_ui_load_screen(SCREEN002);
	}
	
	if(vision_delay_flag)
	{
		if(sound_state==0)
				PWM_Stop(PWM1_MSK);

		//printf("---vision_delay_count : %d\r\n", vision_delay_count);
		vision_delay_count++;
		if(vision_delay_count>=24)//延时3秒
		{
			vision_delay_count = 0;
			vision_delay_flag = false;
			
			vision_state_temp = vision_state;
			show_flag = 1;
		}
	}
	
	if(vision_state_temp==0x03||vision_state_temp==0x04)
	{
		 if(shanshuo_flag)
		 {
			 shanshuo_flag = false;
		 }
		 else
		 {
			 shanshuo_flag = true;
		 }
		 show_flag = 1;
  }
}


static void screen001_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */
		lv_obj_t *Pic;
		Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE5);
		lv_obj_set_hidden(Pic, true); //hidden
		Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE6);
		lv_obj_set_hidden(Pic, true); //hidden
		if(km_MPH_state==0)
				Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE6);
		else
				Pic = synwit_ui_find_lv_obj(WIDGET_IMAGE5);
		lv_obj_set_hidden(Pic, false); 
    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    synwit_ui_start_scr_timer(125, NULL);
		//synwit_ui_start_scr_timer(25, NULL);
}

static void screen001_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen001_cb_obj = {
    .on_screen_start = screen001_start,
    .on_screen_stop = screen001_stop,
    .on_screen_timer = screen001_on_timer,
};

